<template>
  <div id="AsnDetailList">
    <v-tableTitle :btnConfig="btnConfig"
                  :tableSelectData="selectData"
                  @backList="backList"
                  @del="del"
                  @newCreate="newCreate"
                  @exportFile="exportFile"
                  @upFile="file"
                  @mirringdetailgenenalnexttask="mirringdetailgenenalnexttask"
                  @save="save"></v-tableTitle>
    <div class="detail"
         v-if="flag">
      <component :data="{detailData,argument}"
                 :is="currentComponent"
                 ref="com"></component>
    </div>
    <iframe :src="src"
            frameborder="0"
            width="100%"
            height="400px"
            v-else></iframe>
  </div>

</template>

<script>
import tableTitle from "commons/TableTitle";
import navMenu from "commons/NavMenu";
import baseMixin from "@/mixins/BaseMixin";
import config from "../Config"

export default {
  name: "DetailList",
  mixins: [baseMixin, config],
  data () {
    return {
      flag: true,
      newFlag: false,
      src: "",
      argument: {},
      detailData: {},
      selectData: [],
      detailNav: "1",
      componentConfig: [
        {
          name: "detailTable",
          label: "",
          component: () => import('./page/detailTable.vue')
        },
        {
          name: "tableDetail",
          label: this.$t('common.detail'),
          component: () => import('./page/tableDetail.vue')
        },
      ],
    }
  },
  computed: {
    btnConfig () {
      if (this.$store.state.newFlag) {
        return []
      } else if (this.detailNav == 1) {
        return [
          // { label: this.$t('common.create'), certigier: this.newStatus, invalid: "newCreate" },
          { label: this.$t('common.export'), certigier: this.deleteStatus||!this.btnPermNameList.includes('WM06040215'), disabled: true, invalid: "exportFile" },
          { type: "file1", certigier: this.poiStatus||!this.btnPermNameList.includes('WM06040216'), label: this.$t('common.import'), invalid: "file" },
          { label: this.$t('common.delete'), certigier: this.deleteStatus||!this.btnPermNameList.includes('WM06040217'), disabled: true, invalid: "del" },

          {
            label: this.$t('ex.createtask'),
            certigier: this.nextTaskStatus||!this.btnPermNameList.includes('WM06040218'),
            disabled: true,
            invalid: "mirringdetailgenenalnexttask"
          },
        ]
      } else {
        return [
          { label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM06040219'), invalid: "save" },
          { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM06040220'), invalid: "backList" },
        ]
      }
    },
    currentComponent () {
      return this.componentConfig[this.detailNav - 1].component;
    }
  },
  methods: {
    //导入
    async file (data) {
      try {
        let res = await this.$api.post('/importNexttask', data);
        
          this.$message.success(this.$t('app.importMes'));
          this.$children[0].getTableData(1, {})
      } catch (e) {
        this.$message.error(e);
      }
    },
    //导出
    async exportFile (data) {
      try {
        let res = await this.$api.post('/exportNexttask', data, { responseType: 'blob' })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, this.$t('work.ccmirroring') + '.xls');
        } else {
          var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
          var filename = this.$t('work.ccmirroring') + '.xls';
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      }
    },
    async mirringdetailgenenalnexttask (data) {
      try {
        let res = await this.$api.post(`/mirringDetailGenenalNextTask`, data);
        
          this.$message.success(res.returnMgr);
          this.$children[0].getTableData(1, {})
        
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.$children[0].$refs.table.delSelectData(data);
      }
    },
    //删除数据
    del (data) {
      this.$api.post(`/deleteRtx_ccmirroringdetail`, data).then(res => {
        
          this.$message.success(res.returnMgr);
          this.$refs.com.deletTable(data);
          this.$refs.com.getTableData(1, {});
        
      })


    },
    //选中的数据
    setSelectData (data) {
      this.selectData = data;
    },
    //返回试图
    backList () {
      this.detailNav = "1";
    },
    //新建
    async newCreate () {
      // this.detailNav = '2'
      console.log("新建")
    },
    async save () {
      try {
        let res = await this.$api.post(`/saveRtx_ccmirroringdetail/${this.detailData.serialkey}`, this.detailData);
        
          this.$message.success(res.returnMgr);
          this.detailNav = "1";
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async setdialog (data) {
      try {
        let res = await this.$api.get(`/queryRtx_ccmirroringdetailById/${data.serialkey}/${data.subkey}`);
        
          this.detailData = res.bean;
          this.detailNav = "2";
        
      } catch (e) {
        this.$message.error(e);
      }
    },
    async geturl () {
      try {
        let res = await this.$api.get(`/getHttpUrl/LADYCC`);
        
          this.flag = false;
          this.src = `${res.bean}?whseid=${this.$store.state.wms}&opeuser=${this.$Cookies.get('user')}&screenId=${this.$parent.screenid}`
        
      } catch (e) {
        this.$message.error(e);
      }
    },
  },
  mounted () {
    this.newFlag = this.$store.state.newFlag;
    if (this.newFlag) {
      this.flag = false;
    } else if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.argument = data.routeData;
      this.detailData = data.detailList;
      this.detailNav = data.detailListNav;
      if (data.newFlag) {
        this.flag = false;
      }
    } else {
      this.argument = this.$store.state.routeData;
    }
    if (this.$parent.flag) {
      this.geturl()
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-navMenu": navMenu,
  },
  watch: {
    selectData (data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteStatus = this.nextTaskStatus = false;
        } else {
          this.deleteStatus = this.nextTaskStatus = false;
        }
      }
    }
  }
}
</script>

<style>
</style>
